<?php
define("ALIGN_LEFT", 0);
define("ALIGN_CENTER", 1);
define("ALIGN_RIGHT", 2);
define("VALIGN_TOP", 0);
define("VALIGN_MIDDLE", 1);
define("VALIGN_BOTTOM", 2);

function imagestringbox(&$image, $font, $left, $top, $right, $bottom, $align, $valign, $leading, $text, $color) {
// Get size of box
$height = $bottom - $top;
$width = $right - $left;

// Break the text into lines, and into an array
$lines = wordwrap($text, floor($width / imagefontwidth($font)), "\n", true);
$lines = explode("\n", $lines);

// Other important numbers
$line_height = imagefontheight($font) + $leading;
$line_count = floor($height / $line_height);
$line_count = ($line_count > count($lines)) ? (count($lines)) : ($line_count); 

// Loop through lines
for ($i = 0; $i < $line_count; $i++) {
// Vertical Align
switch ($valign) {
case VALIGN_TOP: // Top
$y = $top + ($i * $line_height);
break;
case VALIGN_MIDDLE: // Middle
$y = $top + (($height - ($line_count * $line_height)) / 2) + ($i * $line_height);
break;
case VALIGN_BOTTOM: // Bottom
$y = ($top + $height) - ($line_count * $line_height) + ($i * $line_height);
break;
default:
return false;
}

// Horizontal Align
$line_width = strlen($lines[$i]) * imagefontwidth($font);
switch ($align) {
case ALIGN_LEFT: // Left
$x = $left;
break;
case ALIGN_CENTER: // Center
$x = $left + (($width - $line_width) / 2);
break;
case ALIGN_RIGHT: // Right
$x = $left + ($width - $line_width);
break;
default:
return false;
}

// Draw
imagestring($image, $font, $x, $y, $lines[$i], $color);
}

return $image;
}

// Рисует Бары
function picBar($image,$X,$Y,$X2,$Y2,$color){
	ImageFilledRectangle($image,$X,$Y,$X2,$Y2,$color);
}

function picLine($image,$X,$Y,$X2,$Y2,$color){
	ImageLine($image,$X,$Y,$X2,$Y2,$color);
}

function picFillPoly($image,$X,$Y,$X2,$Y2,$color){
	global $picHeight;
	imagefilledpolygon($image,array($X,$Y,$X2,$Y2,$X2,$picHeight-80,$X,$picHeight-80),4,$color);
}

/* константы */
	$picWidth=600;
	$picHeight=400;
	$countY=10;
	$i = 10;
	$j = 50;
	$k = 10;
	if (!$_GET["picMode"]) $picMode=1;
	
/* текущий день */
	$cur_d = date("d");
/* открытие файла статистики */
        $log_fl = "../../usr/dump/graph.log";
        $log_buf = file($log_fl);
		$dates= Array();
		$IPs= Array();

		foreach ($log_buf as $tmp){
			$tmpi=split(" ",$tmp);
			$dates[]=$tmpi[0];
			$IPs[]=(int)$tmpi[1];
			$HITs[]=(int)$tmpi[2];
			

		}
		$maxIP=max($IPs);
		$maxHIT=max($HITs);
		$maxY=max($maxIP,$maxHIT);
		$maxY=round($maxY+$maxY/($countY-1));
		$dateCount=sizeOf($dates);
		$step=round(($picWidth-40)/$dateCount);
		$stepHeight= round(($picHeight-100)/$countY);
		
		
/* не кешировать */
        header("Cache-Control: no-cache, must-revalidate");
/* создание рисунка */
	header("Content-type: image/PNG");
	$image = ImageCreate($picWidth,$picHeight);
/* цвета */
	$back_col = ImageColorAllocate($image,255,255,255);
	$title_col = ImageColorAllocate($image,0,255,0);
	$coord_col = ImageColorAllocate($image,230,230,230);
	$graf_col1 = ImageColorAllocate($image,255,0,0);
	$graf_col2 = ImageColorAllocate($image,0,0,255);
	$bord_col = ImageColorAllocate($image,0,0,0);
	$text_col = ImageColorAllocate($image,128,0,0);

	
	for ($i=0; $i<=$countY; $i++){
		ImageLine($image,40,$picHeight-80-$i*$stepHeight,$picWidth-10,$picHeight-80-$i*$stepHeight,$coord_col);
		ImageLine($image,38,$picHeight-80-$i*$stepHeight,42,$picHeight-80-$i*$stepHeight,$bord_col);
		imagestringbox($image, 1, 1, $picHeight-80-3-$i*$stepHeight , 35, $picHeight-80-3-$i*$stepHeight + 10, ALIGN_RIGHT, VALIGN_TOP, 2,round($maxY/$countY*$i),$text_col);
		
//			ImageString($image,1,10,$picHeight-80-3-$i*$stepHeight,$maxY/$countY*$i,$text_col);
	}
	$oldX=false;
	$oldY=false;
	$oldY2=false;
	if($maxY)
	for($i=0; $i<$dateCount; $i++){
		$height=round((($picHeight-100)/$maxY)*$IPs[$i]);
		$height2=round((($picHeight-100)/$maxY)*$HITs[$i]);
		
//			echo $height."!";
		ImageLine($image,$step*$i+40,20,$step*$i+40,$picHeight-80,$coord_col);
		ImageLine($image,$step*$i+40,$picHeight-82,$step*$i+40,$picHeight-78,$bord_col);
		
		if (strlen($dates[$i])>2)
			ImageStringUp($image,1,$step*$i+36,$picHeight-20,$dates[$i],$text_col);
		else ImageStringUp($image,1,$step*$i+36,$picHeight-60,$dates[$i],$text_col);
		$X=$step*$i+40;
		$Y=$picHeight-80-$height;
		$Y2=$picHeight-80-$height2;
		if ($oldX){
			if ($picMode==1){
				picLine($image,$oldX,$oldY2,$X,$Y2,$graf_col1);
				picLine($image,$oldX,$oldY,$X,$Y,$graf_col2);
			}
			if ($picMode==3){
				picFillPoly($image,$oldX,$oldY2,$X,$Y2,$graf_col1);
				picFillPoly($image,$oldX,$oldY,$X,$Y,$graf_col2);
			}
		}
		$oldX=$X;
		$oldY=$Y;
		$oldY2=$Y2;
		if ($picMode==2){
			picBar($image,$X,$Y2,$X+$step,$picHeight-80,$graf_col1);
			picBar($image,$X,$Y,$X+$step,$picHeight-80,$graf_col2);
		}
    }
/* вывод рисунка */
	ImageLine($image,40,18,40,$picHeight-78,$bord_col);
	ImageLine($image,38,$picHeight-80,$picWidth-8,$picHeight-80,$bord_col);
	
	ImageFilledRectangle($image,$picWidth-50,27,$picWidth-42,29,$graf_col2);
	ImageString($image,1,$picWidth-35,25,"IP'S",$graf_col2);
	ImageFilledRectangle($image,$picWidth-50,37,$picWidth-42,39,$graf_col1);
	ImageString($image,1,$picWidth-35,35,"HITS",$graf_col1);
	if ($_GET["title"]) ImageString($image,2,40,3,$_GET["title"],$title_col);
	ImagePNG($image);
	ImageDestroy($image);
?>
